package com.starnet.sql;

import java.util.ArrayList;

import com.starnet.constant.DataListTable;
import com.starnet.vo.DataListVo;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class HandlerDataListTable {

	 /**
     * 插入未发送成功的数据
     * */
	synchronized public static void insertDataToDataListTable(String dataId,String dataString,Context context)
    {
		try 
		{
	        ContentValues cv = new ContentValues();
	        cv.put(DataListTable.ID,dataId);
	        cv.put(DataListTable.DATASTRING, dataString);

	        SQLiteDatabase sqLiteDatabase = SqliteOpenHelperClass.getInstance(context).getReadableDatabase();

	        Cursor cursor = sqLiteDatabase.rawQuery("select * from "+DataListTable.TABLENAME+" where "+DataListTable.ID +" = ?",new String[]{dataId+""});

	        if(!cursor.moveToNext())
	        {
	            //没有这条数据，才插入
	            sqLiteDatabase.insert(DataListTable.TABLENAME, null, cv);
	        }
	        
	        cursor.close();
			
		} catch (Exception e) 
		{
			e.printStackTrace();
		}
    }
	
    /**
     * 返回未发送的数据
     * @param context
     * @return
     */
    synchronized public static ArrayList<DataListVo> getDataListRecord(Context context)
    {
    	ArrayList<DataListVo> dVos = new ArrayList<DataListVo>();
    	try 
    	{
    		SQLiteDatabase sqLiteDatabase = SqliteOpenHelperClass.getInstance(context).getReadableDatabase();
	        Cursor cursor = sqLiteDatabase.query(DataListTable.TABLENAME, null, null, null, null, null, null);
	        while (cursor.moveToNext())
	        {
	            String dataId = cursor.getString(cursor.getColumnIndex(DataListTable.ID));
	            String dataString = cursor.getString(cursor.getColumnIndex(DataListTable.DATASTRING));
	            
	            DataListVo vo = new DataListVo();
	            vo.dataId = dataId;
	            vo.dataString = dataString;
	            dVos.add(vo);
	        }
		} catch (Exception e) 
		{
			e.printStackTrace();
		}
        return dVos;
    }
    
    /**
	 * 上传成功后，删除数据
	 */
	synchronized public static void delete(String dataId,Context context)
	{
		try 
		{
			SQLiteDatabase database = SqliteOpenHelperClass.getInstance(context).getReadableDatabase();
			database.delete(DataListTable.TABLENAME, DataListTable.ID+"=?", new String[] { dataId+"" });
			database.close();
		} catch (Exception e) 
		{
		}
	}
	
	 

}
